<?php
class ModelModuleTreodenCpanel extends Model {
	public function treodenGetOption($store_id, $name){
		$query = $this->db->query("SELECT value FROM " . DB_PREFIX . "treoden_cpanel_options  WHERE name = '".$name."' AND store_id='".(int)$store_id."'");
		return $query->row ? $query->row : false;
	}
	public function treodenGetLayout($store_id, $id){
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "treoden_cpanel_layout  WHERE layout_id = '".$id."' AND store_id='".(int)$store_id."'");
		return $query->row ? $query->row : false;
	}
	function treodenGetLayoutId($obj){
		$layout_id = 0;
		if (isset($obj->request->get['route'])) {
			$route = (string)$obj->request->get['route'];
		} else {
			$route = 'common/home';
		}
		if ($route == 'product/category' && isset($obj->request->get['path'])) {
			$path = explode('_', (string)$obj->request->get['path']);
				
			$layout_id = $obj->model_catalog_category->getCategoryLayoutId(end($path));			
		}
		
		if ($route == 'product/product' && isset($obj->request->get['product_id'])) {
			$layout_id = $obj->model_catalog_product->getProductLayoutId($obj->request->get['product_id']);
		}
		
		if ($route == 'information/information' && isset($obj->request->get['information_id'])) {
			$layout_id = $obj->model_catalog_information->getInformationLayoutId($obj->request->get['information_id']);
		}
		
		if (!$layout_id) {
			$layout_id = $obj->model_design_layout->getLayout($route);
		}
						
		if (!$layout_id) {
			$layout_id = $obj->config->get('config_layout_id');
		}
		return $layout_id;
	}
	function treodenGetModuleData($obj, $position, $layout_id){
		$module_data = array();
		
		$obj->load->model('setting/extension');
		
		$extensions = $obj->model_setting_extension->getExtensions('module');		
		
		foreach ($extensions as $extension) {
			$modules = $obj->config->get($extension['code'] . '_module');
		
			if ($modules) {
				foreach ($modules as $module) {
					if ($module['layout_id'] == $layout_id && $module['position'] == $position && $module['status']) {
						$module_data[] = array(
							'code'       => $extension['code'],
							'setting'    => $module,
							'sort_order' => $module['sort_order']
						);				
					}
				}
			}
		}
		$sort_order = array(); 
	  
		foreach ($module_data as $key => $value) {
      		$sort_order[$key] = $value['sort_order'];
    	}
		
		array_multisort($sort_order, SORT_ASC, $module_data);
		return $module_data;
	}
	public function treodenLoadGrid($store_id, $obj, $layout_id, $position, $modules, $mainWidth){
		$layout_info = $this->treodenGetLayout($store_id, $layout_id);
		$term = 0;
		if($layout_info) : 
			$content = $layout_info[$position];
			if($content) : 
				$content = json_decode($content, true);
				// echo "<pre>";
					//// print_r($content_top);
				// echo "</pre>";
				foreach($content as $key=>$value) : 
					?>
					<div class="pure-g-r">
						<?php if(count($value)==1) : ?>
							<?php $sort_order_start = $value[0]['sort_order_start'] ;?>
							<?php $sort_order_end = $value[0]['sort_order_end'] ;?>
							<?php $module_data = array();?>
							<?php foreach($modules as $key=>$module) : ?>
								<?php
									if( $module['sort_order'] >= $sort_order_start and $module['sort_order'] <= $sort_order_end )
									$module_data[] = $module;
								?>
							<?php endforeach;?>
							<?php
								foreach ($module_data as $module) {									
									if ($module['module_output']) {
										echo $module['module_output'];
									}
								}
							?>
							<?php else : ?>
							<?php $i=0;?>
							<?php foreach($value as $item) : ?>
									<?php
										$i++;
										$span = '';
										$inner = '';
										$width = $mainWidth + 20;
										if($item['span']=='span12'){
											$span = 'pure-u-1';
											$width = $mainWidth + 20;
										}	
										if($item['span']=='span9'){
											$span = 'pure-u-3-4';
											$width = round(3*($mainWidth + 20)/4, 0, PHP_ROUND_HALF_DOWN);
										}	
										if($item['span']=='span8'){
											$span = 'pure-u-2-3';
											$width = round(2*($mainWidth + 20)/3, 0, PHP_ROUND_HALF_DOWN);
										}	
										if($item['span']=='span6'){
											$span = 'pure-u-1-2';
											$width = round(1*($mainWidth + 20)/2, 0, PHP_ROUND_HALF_DOWN);
										}	
										if($item['span']=='span4'){
											$span = 'pure-u-1-3';
											$width = round(1*($mainWidth + 20)/3, 0, PHP_ROUND_HALF_DOWN);
										}	
										if($item['span']=='span3'){
											$span = 'pure-u-1-4';
											$width = round(1*($mainWidth + 20)/4, 0, PHP_ROUND_HALF_DOWN);
										}	
										if($i==1)
											$inner =" alpha";
										if($i==count($value))
											$inner =" omega";	
									?>
									<div class="<?php echo $span;?> modules-container" data-width = "<?php echo $width?>">
									<div class="col-inner <?php echo $inner;?>">
										<?php $sort_order_start = $item['sort_order_start'] ;?>
										<?php $sort_order_end = $item['sort_order_end'] ;?>
										<?php $module_data = array();?>
										<?php foreach($modules as $key=>$module) : ?>
											<?php
												if( $module['sort_order'] >= $sort_order_start and $module['sort_order'] <= $sort_order_end )
												$module_data[] = $module;
											?>
										<?php endforeach;?>
										<?php
											foreach ($module_data as $module) {									
												if ($module['module_output']) {
													echo $module['module_output'];
												}
											}
										?>
									</div>
									</div>
							<?php endforeach;?>
						<?php endif;?>
					</div>
					<div class="clear"></div>
					<?php
				endforeach;
			else : 
				$term = 1;
			endif;	
		else : 
			$term = 1;
		endif;	
		if($term ==1 )	
			foreach ($modules as $module) {									
				if ($module['module_output']) {
					echo $module['module_output'];
				}
			}			
	}				public function getSetting($store_id){		$data = array(); 				$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "treoden_cpanel_options WHERE store_id = '" . (int)$store_id . "'");				foreach ($query->rows as $result) {				$data[$result['name']] = $result['value'];		}		return $data;	}
}	
?>